package org.itboys.weixin.tools;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.json.JsonUtils;
import org.itboys.weixin.constant.WeixinConstants;
import org.itboys.weixin.dto.WeixinResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信模板消息
 * @author 江雄
 *
 * 2017年12月19日
 */
public class WeiXinTemplateMessageUtils {
	
	protected static Logger logger = LoggerFactory.getLogger(WeiXinTemplateMessageUtils.class);

	/**
	 *
	 * @param nickName
	 * @param title
	 * @param content
	 * @param remark
	 */
	public static void send(String openId,String nickName,String title,String content,String remark,String url){
		String accessToken=	WeixinUtils.getAccessToken("wx849502424d6ba19e",null);

		//发送模板消息
		//return null;

		Map<String,Object> param = new HashMap<String,Object>();
		param.put("touser",openId);
		param.put("template_id","XCKTbXiufsOQRkeT8rUOGqv9EEwnNcCp6Wg7X1pYCTg");
		param.put("url",url);
		Map<String,Object> data = new HashMap<String,Object>();
		Map<String,Object> first = new HashMap<String,Object>();
		first.put("value","尊敬的，"+nickName+"。你有一条新的消息通知");
		first.put("color","#173177");
		data.put("first",first);
		Map<String,Object> keyword1 = new HashMap<String,Object>();
		keyword1.put("value",title);
		keyword1.put("color","#173177");
		data.put("keyword1",keyword1);
		Map<String,Object> keyword2 = new HashMap<String,Object>();
		keyword2.put("value",content);
		keyword2.put("color","#173177");
		data.put("keyword2",keyword2);
		Map<String,Object> remarkMap= new HashMap<String,Object>();
		remarkMap.put("value",remark);
		remarkMap.put("color","#173177");
		data.put("remark",remarkMap);
		param.put("data",data);
		String json=JsonUtils.objectToString(param);
		logger.info("-----json--"+json);
		//String accessToken = WeixinUtils.getAccessToken(appId, appSecret);
		String createMenuUrl = WeixinConstants.URL.TEMPLATE_SEND_MESSAGE+accessToken;
		PostMethod postMethod = new PostMethod(createMenuUrl);
		RequestEntity requestEntity = null;
		try {
			requestEntity = new StringRequestEntity(json, CommonConstants.CONTENT_TYPE.JSON_TYPE ,CommonConstants.ENCODE.UTF_8);
			postMethod.setRequestEntity(requestEntity);
			HttpClient httpclient = HttpHelper.getHttpClient();
			int statusCode = httpclient.executeMethod(postMethod);
			if (statusCode != HttpStatus.SC_OK) {
				//logger.error("doCreateWeixinMenu error statusCode={} appId={} appScret={} jsonaMenu={}",new Object[]{statusCode,appId,appSecret,jsonMenu});
				//return -1;
			}
			String createResult = postMethod.getResponseBodyAsString();
			logger.info("_------------"+createResult);
			WeixinResult result = JsonUtils.jsonToObject(WeixinResult.class, createResult);
			if(result.getErrcode()!=0){
				//logger.error("doCreateWeixinMenu return error errorCode={} errmsg={} appId={} appScret={} jsonMenu={}",new Object[]{result.getErrcode(),result.getErrmsg(),appId,appSecret,jsonMenu});
				//return -2;
			}
			//return 1;//成功
		} catch (Exception e) {
			//logger.error("doCreateWeixinMenu Exception appId="+appId+",appScret="+appSecret+",jsonMenu="+jsonMenu,e);
			//return 0;
		}finally{
			if(postMethod!=null){
				postMethod.releaseConnection();
			}
		}
	String result=	HttpHelper.doPost(WeixinConstants.URL.TEMPLATE_SEND_MESSAGE+accessToken,param);
		logger.info("result="+result);
	}
	public static void main(String[] args) {
		send("oq9nTwUkVqhNedFXyOK6GX5oX_9E","测试名称","测试标题","测试内容","测试备注","http://www.baidu.com");
	}
}
